﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;

using DNA;
using Microsoft.Win32;
using System.Diagnostics;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using TJADRICD.CollaborativeDesignPlugin.Function;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;
using DBTransMan = Autodesk.AutoCAD.DatabaseServices.TransactionManager;
using Application = Autodesk.AutoCAD.ApplicationServices.Application;
using System.Runtime.InteropServices;

namespace TJADRICD.CollaborativeDesignPlugin.Function
{
    public static class Function_Registry
    {
        private static List<String> GetVersionList()
        {
            List<String> list = new List<String>();
            RegistryKey current_user = Registry.LocalMachine;
            RegistryKey software = current_user.OpenSubKey("Software");
            RegistryKey autodesk = software.OpenSubKey("Autodesk");
            RegistryKey autocad = autodesk.OpenSubKey("AutoCAD");
            String[] version_list = autocad.GetSubKeyNames();
            foreach (String version_name in version_list)
            {
                RegistryKey version = autocad.OpenSubKey(version_name);
                String[] product_list = version.GetSubKeyNames();
                foreach (String product_name in product_list)
                {
                    RegistryKey product = version.OpenSubKey(product_name);
                    object temp = product.GetValue("AcadLocation");
                    String acadlocation = String.Empty;
                    if (temp == null)
                    {
                        break;
                    }
                    else
                    {                        
                        acadlocation = temp.ToString();
                    }
                    DirectoryInfo acad_dir = new DirectoryInfo(acadlocation);
                    Boolean exists = File.Exists(acad_dir.FullName + @"\" + @"acad.exe");
                    if(exists)
                    {
                        String key = product.Name;
                        String local_machine = "HKEY_LOCAL_MACHINE\\";                        
                        list.Add(key.Substring(local_machine.Length));
                    }
                }
            }
            return list;
        }

        public static Dictionary<String, String> GetOptions()
        {
            Dictionary<String, String> table = new Dictionary<String, String>();
            String PrinterConfigDir = String.Empty;
            String PrinterDescDir = String.Empty;
            String PrinterStyleSheetDir = String.Empty;
            List<String> list = GetVersionList();
            if (list.Count == 0)
            {
                MessageBox.Show("检查到注册表信息损坏");
            }
            else if (list.Count >=1)
            {
                list.Clear();
                list.Add(@"Software\Autodesk\AutoCAD\R18.0\ACAD-8001:804");
            }
            if (list.Count == 1)
            {
                String key_name = list.First() + @"\Profiles";
                RegistryKey key = Registry.CurrentUser.OpenSubKey(key_name);

                String profile_name = key.GetValue("").ToString();
                RegistryKey profile = key.OpenSubKey(profile_name + @"\General");
                PrinterConfigDir = profile.GetValue("PrinterConfigDir").ToString();
                PrinterDescDir = profile.GetValue("PrinterDescDir").ToString();
                PrinterStyleSheetDir = profile.GetValue("PrinterStyleSheetDir").ToString();
                table.Add("PrinterConfigDir", PrinterConfigDir);
                table.Add("PrinterDescDir", PrinterDescDir);
                table.Add("PrinterStyleSheetDir", PrinterStyleSheetDir);
            }
            return table;
        }
    }
}